//
//  QRCodeScanViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/5/2.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "QRCodeScanViewController.h"
#import "WebViewController.h"

#import "HWZBAPIRequest.h"
#import "Context.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "MBProgressHUD+Toast.h"


@interface QRCodeScanViewController () <UIAlertViewDelegate>

@end

@implementation QRCodeScanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 注册观察者
    [SGQRCodeNotificationCenter addObserver:self selector:@selector(SGQRCodeInformationFromeAlbum:) name:SGQRCodeInformationFromeAibum object:nil];
    [SGQRCodeNotificationCenter addObserver:self selector:@selector(SGQRCodeInformationFromeScanning:) name:SGQRCodeInformationFromeScanning object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)SGQRCodeInformationFromeAlbum:(NSNotification *)notification {
    SGQRCodeLog(@"noti - - %@", notification);
    NSString *string = notification.object;
    NSLog(@"扫描结果 -- %@", string);
    
//    ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init];
//    jumpVC.jump_URL = string;
//    [self.navigationController pushViewController:jumpVC animated:YES];
}

- (void)SGQRCodeInformationFromeScanning:(NSNotification *)notification {
    SGQRCodeLog(@"noti - - %@", notification);
    NSString *string = notification.object;
    
    if ([string hasPrefix:@"http"]) {
        WebViewController *webViewController = [[WebViewController alloc] init];
        webViewController.urlStr = string;
        [self.navigationController pushViewController:webViewController animated:YES];
    } else if([string hasPrefix:@"huawei://"]) { // 扫描结果为条形码
        
        NSString *userId = [string substringFromIndex:(@"huawei://".length - 1)];
        [self inviteAnotherPeople:userId];
    } else {
        NSString *scanResultStr = [NSString stringWithFormat:@"扫描结果:\n%@", string];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:scanResultStr delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
}

- (void)dealloc {
    SGQRCodeLog(@"QRCodeScanningVC - dealloc");
    [SGQRCodeNotificationCenter removeObserver:self];
}

- (void)inviteAnotherPeople:(NSString *)userId {
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:userId forKey:@"id"];
    [SVProgressHUD showWithStatus:@"正在邀请用户..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] getQRCodeInviteWithParams:params success:^(InviteResultInfo *resultInfo) {
        [SVProgressHUD dismiss];
        if(resultInfo != nil) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"邀请用户成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"邀请用户失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];

        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"邀请用户失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        
    }];
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


@end
